<?php

namespace Modules\Chinadmin\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Modules\Chinadmin\Models\AccountModel;
use Modules\Chinadmin\Models\CompanyModel;
use Modules\Chinadmin\Models\FormModel;

class Account extends Base
{

    /**
     * 依赖注入
     * http://l.com/chinadmin/admin/index/index#//chinadmin/admin/Account/index
     */
    public function __construct(Request $request){

        //自行配制
        $this->models = new AccountModel();
        $this->with = [];
        parent::__construct();

    }

    /**
     * 添加和编辑
     * default 来决定默认值
     * http://cheyoukong.1zhetejia.com/chinadmin/admin/Account/create_partner
     * todo::这里
     *
     */
    public function create_partner(Request $request){

        $i = $request->input();

        unset($i['MT']);
        $data = $this->createData($request,$i);
        return view('chinadmin::admin.' . $this->class . '.create_partner',$data);

    }

    /*
     * 针对本账号定位所在公司。
     *
     */
    protected function sonCreate($data){

        $user = Auth::guard('admin')->user();
        $Company = CompanyModel::where('company_id',$user['company_id'])->first();

        if(empty($data['this_data']['company_id_show'])){
            unset($data['this_data']['company_id_show']);
        }

        $data['this_data']['company_id'] = $data['this_data']['company_id']??$user['company_id'];
        $data['this_data']['p_company']['company_name'] = $data['this_data']['p_company']['company_name']??$Company['company_name'];
        $data['this_data']['company_id_show'] = $data['this_data']['company_id_show']??$Company['company_name'];             //默认选中

        return $data;

    }

    public function sonCreateAjaxBegin($i){

        //对于密码的处理
        if(isset($i['password']) && $i['password'] != ''){
            $i['password'] = Hash::make($i['password']);
        }else{
            unset($i['password']);
        }

        //if(!isset($i[$this->models->primaryKey])){}
        $Account = AccountModel::where("username",$i['username'])
            ->where("id","<>",$i[$this->models->primaryKey])
            ->first();
        if(!empty($Account)){
            mdsResponse(400,"此账号名已经使用，请更换一个");
        }
        $Account = AccountModel::where("phone",$i['phone'])
            ->where("id","<>",$i[$this->models->primaryKey])
            ->first();
        if(!empty($Account)){
            mdsResponse(400,"此手机号已经使用，请更换一个");
        }

        return $i;

    }

    /**
     *  登录人员管理
     * /chinadmin/admin/account/companyWhiteManage
     */
    public function companyWhiteManage(Request $request){
        $i = $request->input();
        $data = $this->indexData($i,$request);
        return view('chinadmin::admin/Account/companyWhiteManage',$data);
    }




}
